import request from 'common/request'

const getProducts = async function(page = 1) {
	console.log(request)
	let url = `https://api.beidian.com/mroute.html?method=beidian.h5.shop.product.list&page=${page}&shop_id=682731`;
	console.log(url);
	const data = await request.get(url);
	if (data.has_more) {
		return data.shop_products;
	} else {
		return false;
	}
}

const search = async function(keywords, page = 1) {
	console.log("keywords:" + keywords)
	//https://api.beidian.com/mroute.html?method=beidian.search.item.list&keyword=%E7%94%B5%E8%84%91&sort=hot&page_size=20&page=3
	keywords = encodeURI(keywords);
	let url =
		`https://api.beidian.com/mroute.html?method=beidian.search.item.list&keyword=${keywords}&sort=hot&page_size=20&page=${page}`;
	console.log(url);
	const data = await request.get(url);
	// console.log('data:' + JSON.stringify(data));
	// console.log(data.has_more)
	if (data.has_more) {
		return data.items;
	} else {
		console.log("没有数据了！")
		return false;
	}
}
// app/store应用的服务程序
module.exports = {
	getProducts,
	search,
}
